package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.jobseeker.PageJobseekerRequest;
import zengjin.model.dto.jobseeker.UpdateJobseekerPersonalDto;
import zengjin.model.en.Image;
import zengjin.model.en.Jobseeker;
import zengjin.model.vo.jobseeker.JobseekerPersonalVO;
import zengjin.model.vo.jobseeker.JobseekerVO;
import zengjin.service.ImageService;
import zengjin.service.JobseekerService;

/**
 * @author 19324
 */
@Tag(name = "求职者相关接口")
@RestController
@RequestMapping("/jobseeker")
@Slf4j
public class JobseekerController {

    @Resource
    private JobseekerService jobseekerService;

    @Resource
    private ImageService imageService;

    //1.查询求职者个人信息
    @Operation(summary = "查询求职者个人信息")
    @PostMapping("/queryJobseekerPersonal")
    public R<JobseekerPersonalVO> queryJobseekerPersonal(Long jobseekerId) {
        //1.参数校验
        if (jobseekerId == null) {
           throw new RuntimeException("参数为空");
        }
        //2.调用service
        Jobseeker jobseeker = jobseekerService.getById(jobseekerId);
        JobseekerPersonalVO jobseekerPersonalVO = BeanUtil.copyProperties(jobseeker, JobseekerPersonalVO.class);
        //3.查询头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "jobseeker_avatar");
        imageQueryWrapper.eq("image_name_id", jobseekerId);
        Image image = imageService.getOne(imageQueryWrapper);
        if (image != null) {
            jobseekerPersonalVO.setUserAvatarUrl(image.getImageUrl());
        }
        return R.success(jobseekerPersonalVO, "查询成功");
    }

    //2.修改求职者个人信息
    @Operation(summary = "修改求职者个人信息")
    @PostMapping("/updateJobseekerPersonal")
    public R<Boolean> updateJobseekerPersonal(@RequestBody UpdateJobseekerPersonalDto updateJobseekerPersonalDto) {
        //1.参数校验
        if (updateJobseekerPersonalDto == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result =jobseekerService.updateJobseekerPersonal(updateJobseekerPersonalDto);
        if (!result) {
            throw new RuntimeException("修改失败");
        }
        return R.success(true, "修改成功");
    }

    //3.查询求职者信息根据ID
    @Operation(summary = "查询求职者信息根据ID")
    @PostMapping("/getJobseekerDetailByJobseekerId")
    public R<JobseekerVO> getJobseekerDetailByJobseekerId(Long jobseekerId) {
        //1.参数校验
        if (jobseekerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Jobseeker jobseeker = jobseekerService.getById(jobseekerId);
        JobseekerVO jobseekerVO = BeanUtil.copyProperties(jobseeker, JobseekerVO.class);
        //查询头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "jobseeker_avatar");
        imageQueryWrapper.eq("image_name_id", jobseekerId);
        Image image = imageService.getOne(imageQueryWrapper);
        if (image != null) {
            jobseekerVO.setUserAvatarUrl(image.getImageUrl());
        }
        return R.success(jobseekerVO, "查询成功");
    }


    //4.管理员修改求职者状态
    @Operation(summary = "管理员修改求职者状态")
    @PostMapping("/updateJobseekerStatus")
    public R<Boolean> updateJobseekerStatus(Long jobseekerId, String jobseekerStatus) {
        //1.参数校验
        if (jobseekerId == null || jobseekerStatus == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        UpdateWrapper<Jobseeker> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("jobseeker_id", jobseekerId)
                .set("jobseeker_status", jobseekerStatus);
        boolean result = jobseekerService.update(null, updateWrapper);
        if (!result) {
            throw new RuntimeException("修改失败");
        }
        return R.success(true, "修改成功");
    }


    //5.管理员-分页获取求职者信息
    @Operation(summary = "管理员-分页获取求职者信息")
    @PostMapping("/getJobseekerAdminPage")
    public R<PageResponse<JobseekerVO>> getJobseekerAdminPage(@RequestBody PageJobseekerRequest pageJobseekerRequest) {
        //1.参数校验
        if (pageJobseekerRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<JobseekerVO> jobseekerAdminPage = jobseekerService.getJobseekerAdminPage(pageJobseekerRequest);
        return R.success(jobseekerAdminPage, "查询成功");
    }

    //6.管理员-修改雇主认证状态
    @Operation(summary = "管理员-修改求职者认证状态")
    @PostMapping("/updateJobseekerVerified")
    public R<Boolean> updateJobseekerVerified(Long jobseekerId, String jobseekerIsVerified) {
        //1.参数校验
        if (jobseekerId == null || jobseekerIsVerified == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = jobseekerService.updateJobseekerVerified(jobseekerId, jobseekerIsVerified);
        if (!result) {
            return R.error(false, "修改求职者认证状态失败");
        }
        return R.success(true, "修改求职者认证状态成功");
    }

}
